﻿Imports System.Data.SqlClient
Imports BUS

Public Class frmBPhan

    Private da As SqlDataAdapter
    Private thaotac As String = "sua"
    Private dt2 As DataTable

    Private Sub updatebophan()
        Dim dt As New DataTable
        dt = dgvbophan.DataSource

        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Update(dt)

    End Sub

    Private Sub EnableText(ByVal b As Boolean)
        txttenbp.Enabled = b
        txtchucnang.Enabled = b
    End Sub

    Private Sub XoaText()
        txttenbp.Text = ""
        txtchucnang.Text = ""
    End Sub

    Private Sub frmBPhan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        EnableText(False)

        Dim dt As New DataTable

        da = BoPhanBus.LayDSDanhSach()


        da.Fill(dt)
        dgvbophan.DataSource = dt
        dt2 = New DataTable
        da.Fill(dt2)
    End Sub

    'Private Sub dgvbophan_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs)
    '    dgvbophan.Rows(e.RowIndex).Cells("stt").Value = e.RowIndex + 1
    'End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        EnableText(True)
        XoaText()
        txttenbp.Focus()
        thaotac = "them"
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        If thaotac = "them" And txttenbp.Text.Trim() <> "" And txtchucnang.Text.Trim() <> "" Then
            Dim dt As New DataTable
            dt = dgvbophan.DataSource

            Dim dr As DataRow = dt.NewRow()
            dr("stt") = dt.Rows.Count + 1
            dr("tenbp") = txttenbp.Text.Trim()
            dr("chucnang") = txtchucnang.Text.Trim()
            dt.Rows.Add(dr)
            dgvbophan.DataSource = dt
            thaotac = "sua"
        End If

        updatebophan()
        dt2 = dgvbophan.DataSource
        EnableText(False)
        XoaText()

        MessageBox.Show("Đã lưu", "Thông báo")

    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click

        Dim digres As DialogResult = MessageBox.Show("Bạn có muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If digres = DialogResult.Yes Then
            Dim item As DataGridViewSelectedRowCollection = dgvbophan.SelectedRows

            For Each row As DataGridViewRow In item
                dgvbophan.Rows.Remove(row)
            Next

            ' updatebophan()
        Else
            Return
        End If

    End Sub


    Private Sub drpbtnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpbtnxoa.Click
        Dim digres As DialogResult = MessageBox.Show("Bạn có muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If digres = DialogResult.Yes Then
            Dim item As DataGridViewSelectedRowCollection = dgvbophan.SelectedRows

            For Each row As DataGridViewRow In item
                dgvbophan.Rows.Remove(row)
            Next

            ' updatebophan()
        Else
            Return
        End If

    End Sub

    Private Sub dgvbophan_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvbophan.KeyDown
        If e.KeyCode = Keys.Delete Then
            Dim digres As DialogResult = MessageBox.Show("Bạn có muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
            If digres = DialogResult.Yes Then
                Dim item As DataGridViewSelectedRowCollection = dgvbophan.SelectedRows
                For Each row As DataGridViewRow In item
                    dgvbophan.Rows.Remove(row)
                Next
                ' updatebophan()

            Else
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub frmBPhan_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim _luu As Boolean = False
        Dim _dt As New DataTable()
        _dt = dt2

        Dim dv2 As New DataView
        dv2 = dt2.DefaultView
        dv2.Sort = "mabp ASC"
        dt2 = dv2.ToTable()

        Dim dt3 As New DataTable
        dt3 = dgvbophan.DataSource
        Dim dv3 As New DataView
        dv3 = dt3.DefaultView
        dv3.Sort = "mabp ASC"
        dt3 = dv3.ToTable()

        If dt2.Rows.Count = dt3.Rows.Count Then
            For i As Integer = 0 To dt2.Rows.Count - 1 Step 1
                If dt2.Rows(i)("tenbp").ToString() <> dt3.Rows(i)("tenbp").ToString() Or dt2.Rows(i)("chucnang").ToString() <> dt3.Rows(i)("chucnang").ToString() Then
                    _luu = True
                    Exit For
                End If
            Next
        Else
            _luu = True
        End If

        If _luu = True Then
            Dim digres As DialogResult = MessageBox.Show("Bạn có muốn lưu không?", "Xác nhận", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
            If digres = DialogResult.Cancel Then
                e.Cancel = True
            ElseIf digres = DialogResult.No Then
                dgvbophan.DataSource = _dt
            Else
                updatebophan()
            End If
        End If
    End Sub

    Private Sub btnexcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class